<?php
namespace app\weixin\controller;
use think\Controller;
use wechat\wechat_msg;//公众号消息回复类
use wechat\wechat_adv;//公众号接口操作类
class Wechat
{
	public $weixin;
	public $msg;
	public $appid     = 'wxf645c16ca353039a';
	public $appsecret = '263356fd1e5f77f72a60853b7dfad4bf';
	public $token     = 'weixin';
	public function index()
	{
		define('TOKEN', $this->token);
		$this->weixin = new wechat_adv($this->appid, $this->appsecret);
		//$this->msg = new wechat_msg($this->appid, $this->appsecret);
		//dump($this->weixin->create_qrcode('QR_SCENE', 6));
		die;
		if (!isset($_GET['echostr']))
		{
			$this->responseMsg();
		}
		else
		{
		    $this->valid();
		}
	}
    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature())
        {
            echo $echoStr;
            exit;
        }
    }
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $token = 'weixin';
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
	//处理消息
    public function responseMsg()
    {
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr))
        {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $RX_TYPE = trim($postObj->MsgType);
            //用户发送的消息类型判断
            switch ($RX_TYPE)
            {
                case "text"://消息为文本消息
        			$this->msg->text($postObj,'asdf');//回复文本消息
                    break;
                case "image"://消息为图片消息
                    
                    break;
                case "voice"://消息为语音消息
                    
                    break;
                case "video"://消息为视频消息
                    
                    break;
				case "shortvideo"://消息为短视频消息
                    
                    break;
				case "location": //消息为地理位置消息    Location_X地理位置纬度         Location_Y	地理位置经度            Scale	地图缩放大小            Label	地理位置信息        MsgId消息id，64位整型
                    
                    break;
				case "link"://消息为链接消息
                    
                    break;	
				case "event"://事件处理
                    $this->receiveEvent($postObj);
                    break;
                default:
                    $result = "unknow msg type: ".$RX_TYPE;
                    break;
            }
            echo $result;
        }else {
            echo "";
            exit;
        }
    }
    //处理事件
	private function receiveEvent($object)
	{
	    switch ($object->Event)
	    {
	        case "subscribe"://扫码关注事件/或者带参数二维码扫描关注事件 EventKey	事件KEY值，qrscene_为前缀，后面为二维码的参数值
	            $this->msg->text($object,'key为:'.$object->EventKey);//回复文本消息
	            break;
			case "unsubscribe"://扫码取消关注事件
	            
	            break;
	        case "SCAN"://已关注扫描带参数二维码事件 EventKey	事件KEY值，qrscene_为前缀，后面为二维码的参数值
	            $this->msg->text($object,'key为:'.$object->EventKey);//回复文本消息
	            break;
			case "LOCATION"://上报地理位置事件  Latitude地理位置纬度    Longitude地理位置经度         Precision	地理位置精度    不允许回复消息 返回success给服务器
	            echo 'success';
				file_put_contents('./aa.txt', '纬度'.$object->Latitude.'经度'.$object->Longitude.'地理位置精度'.$object->Precision);
	            break;
			case "CLICK"://点击自定义菜单事件     EventKey	事件KEY值，与自定义菜单接口中KEY值对应
	            $this->msg->text($object,'key为'.$object->EventKey);//回复文本消息
	            break;
			case "VIEW"://自定义菜单点击菜单跳转链接时的事件推送
	            
	            break;
	        default:
	            break;      
	 
	    }
	}
}